<template>
  <el-dialog :visible.sync="visible" :title="isAdd?'添加':'编辑得分'" width="30%" :destroyOnClose="true"
    :keyboard="false">
    <el-form :model="formData" ref="formData" name="basic" label-width="80px" autocomplete="off">
      <CompanyInput :compShort.sync="formData.compShort" :disabled="!isAdd" />
      <el-form-item label="统计月份" name="scoreMonth">
        <el-date-picker type="month" placeholder="选择月份" v-model="formData.scoreMonth"
          value-format="yyyy-MM" format="yyyy-MM" style="width: 100%;"
          :disabled="!isAdd"></el-date-picker>
      </el-form-item>

      <el-form-item label="得分" name="score">
        <el-input v-model="formData.score" type="number" />
      </el-form-item>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleOk">确 定</el-button>
    </span>

  </el-dialog>
</template>

<script>
import CompanyInput from '@/components/CompanyInput';
export default {
  components: { CompanyInput },
  props: {
    modalText: String,
    saveFun: Function,
    addFun: Function,
    clickQuery: Function,
  },
  data() {
    return {
      isAdd: true,
      formRef: null,
      formData: {},
      visible: false,
    };
  },

  methods: {
    openAddDialog() {
      this.isAdd = true;
      this.visible = true;
      this.formData = {};
    },
    openEditDialog(data) {
      this.isAdd = false;
      this.visible = true;
      Object.assign(this.formData, data);
    },

    handleCancel() {
      this.visible = false;
    },
    handleOk() {
      this.visible = false;
      this.saveData();
    },

    async saveData() {
      let res = null;
      const req = Object.assign({}, this.formData);
      // const startDate = dayjs(formData.startDate_new).format('YYYY-MM-DD')
      if (this.isAdd) {
        res = await this.addFun(req);
      } else {
        res = await this.saveFun(req);
      }
      if (res.success) {
        if (this.clickQuery) {
          res = await this.clickQuery();
        }
      } else {
        this.$message.error(res.rtn_message);
      }
    },
  },
};
</script>